//封装category的业务代码
import { getCategoryAPI } from '@/apis/category';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { onBeforeRouteUpdate } from 'vue-router';
export function useCategory() {
    //获取数据
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)//route.params.id存在滞后性
        categoryData.value = res.result
    }
    onMounted(() => getCategory())

    //路由参数变化时 分类数据接口重新发送
    //利用最新参数to
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)

    })
    return {
        categoryData
    }
}
